//
//  ABAlertView.swift
//  BusinessShow
//
//  Created by yuanwei on 16/6/23.
//  Copyright © 2016年 qeegoo. All rights reserved.
//

import UIKit
import SnapKit

open class APPAlertView: UIView {
    
    typealias clickAlertClosure = (_ index: Int) -> Void //声明闭包，点击按钮传值
    //把申明的闭包设置成属性
    var clickClosure: clickAlertClosure?
    //为闭包设置调用函数
    func clickIndexClosure(closure:clickAlertClosure?){
        //将函数指针赋值给myClosure闭包
        clickClosure = closure
    }
    let whiteView = UIView()
    let titleLabel = UILabel() //显示内容
    let cancelBtn = UIButton() //取消按钮
    let sureBtn = UIButton() //确定按钮
    
    //tipType=1是弹出框 tipType=2是需要传入内容
    init(title: String?, cancelButtonTitle: String = "取消", sureButtonTitle: String = "确定", tipType: Int = 1)
    {
        super.init(frame: CGRect(x: 0, y: 0, width: kScreenW, height: kScreenH))
        createAlertView()

        self.titleLabel.text = title
        self.cancelBtn.setTitle(cancelButtonTitle, for: .normal)
        self.sureBtn.setTitle(sureButtonTitle, for: .normal)
    }
    
    required public init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    //MARK: 背景
    func backGroundView()
    {
        //布局
        self.frame = CGRect(x: 0, y: 0, width: kScreenW, height: kScreenH)
        self.backgroundColor = UIColor.black.withAlphaComponent(0.5)
        
        whiteView.backgroundColor = UIColor.white
        whiteView.layer.cornerRadius = 5
        whiteView.clipsToBounds = true
        self.addSubview(whiteView)
        whiteView.snp.remakeConstraints { (make) in
            make.centerX.equalTo(self.snp.centerX)
            make.centerY.equalTo(self.snp.centerY)
            make.width.equalTo(280)
        }
    }
    
    //MARK:创建（只有标题  取消和确定按钮）
    func createAlertView()
    {
        backGroundView()
    
        titleLabel.textColor = BaseColor.BlackColor
        titleLabel.font = UIFont.systemFont(ofSize: 16)
        titleLabel.textAlignment = .center
        titleLabel.numberOfLines = 0
        whiteView.addSubview(titleLabel)
        titleLabel.snp.remakeConstraints { (make) in
            make.top.equalTo(20)
            make.left.equalTo(10)
            make.right.equalTo(-10)
        }
        addBottomView(constraintLabel: titleLabel)
    }
    
    func addBottomView(constraintLabel: UIView)
    {
        //底部View
        let bottomView = UIView()
        bottomView.backgroundColor = UIColor.clear
        whiteView.addSubview(bottomView)
        bottomView.snp.remakeConstraints { (make) in
            make.top.equalTo(constraintLabel.snp.bottom).offset(10)
            make.left.right.bottom.equalTo(0)
            make.height.equalTo(50)
        }
        //横分割线
        let hLineView = UIView()
        hLineView.backgroundColor = BaseColor.BorderColor
        bottomView.addSubview(hLineView)
        hLineView.snp.remakeConstraints { (make) in
            make.top.left.right.equalTo(0)
            make.height.equalTo(0.7)
        }
        //竖分割线
        let vLineView = UIView()
        vLineView.backgroundColor = BaseColor.BorderColor
        bottomView.addSubview(vLineView)
        vLineView.snp.remakeConstraints { (make) in
            make.top.bottom.equalTo(0)
            make.width.equalTo(0.5)
            make.height.equalTo(bottomView.snp.height)
            make.centerX.equalTo(bottomView.snp.centerX)
        }
        
        //取消按钮
        cancelBtn.backgroundColor = UIColor.clear
        cancelBtn.setTitleColor(BaseColor.BlueColor, for: .normal)
        cancelBtn.titleLabel!.font = UIFont.systemFont(ofSize: 16)
        cancelBtn.layer.cornerRadius = 3
        cancelBtn.clipsToBounds = true
        cancelBtn.tag = 1
        cancelBtn.addTarget(self, action: #selector(APPAlertView.clickBtnAction(sender:)), for: .touchUpInside)
        bottomView.addSubview(cancelBtn)
        cancelBtn.snp.remakeConstraints { (make) in
            make.top.left.equalTo(0)
            make.width.equalTo(bottomView).multipliedBy(0.5)
            make.height.equalTo(bottomView.snp.height)
        }
        
        //确认按钮
        sureBtn.backgroundColor = UIColor.clear
        sureBtn.setTitleColor(BaseColor.BlueColor, for: .normal)
        sureBtn.titleLabel!.font = UIFont.systemFont(ofSize: 16)
        sureBtn.layer.cornerRadius = 3
        sureBtn.clipsToBounds = true
        sureBtn.tag = 2
        sureBtn.addTarget(self, action: #selector(APPAlertView.clickBtnAction(sender:)), for: .touchUpInside)
        bottomView.addSubview(sureBtn)
        sureBtn.snp.remakeConstraints { (make) in
            make.top.right.equalTo(0)
            make.width.equalTo(bottomView).multipliedBy(0.5)
            make.height.equalTo(bottomView.snp.height)
        }
    }
    
    //MARK:按键的对应的方法
    func clickBtnAction(sender: UIButton) {
        if (clickClosure != nil) {
            clickClosure!(sender.tag)
        }
        dismiss()
    }
    //MARK:消失
    func dismiss() {
        UIView.animate(withDuration: 0.3, animations: { () -> Void in
            self.alpha = 0
        }) { (finish) -> Void in
            if finish {
                self.removeFromSuperview()
            }
        }
    }
    //MARK:指定视图实现方法
    func show() {
        UIView.animate(withDuration: 0.3) {
            let wind = UIApplication.shared.keyWindow
            wind?.addSubview(self)
        }
    }
}
